<template>
  <div class="calendar">
    <div style="text-align: right">
      <el-select v-model="year" @change="changeDate">
        <el-option
          v-for="item in yearArr"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="month" @change="changeDate">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <el-calendar :value="value">
      <template #dateCell="{ data }">
        <div @click="changDay(data.day)">
          <span>{{ data.day.split('-')[2] }} </span>
          <span v-if="showDay(data)">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  data() {
    return {
      value: new Date(),
      year: 2021,
      month: 12,
      yearArr: []
    }
  },
  watch: {
    value: {
      handler(vewVal) {
        this.year = vewVal.getFullYear()
        this.month = vewVal.getMonth() + 1
        const min = this.year - 5
        const max = this.year + 5
        this.yearArr = []
        for (let i = min; i <= max; i++) {
          this.yearArr.push(i)
        }
      },
      immediate: true
    }
  },
  methods: {
    showDay(data) {
      // 获取日
      const t = new Date(data.day)
      //   获取今天的日期
      const day = t.getDay()
      const bol = data.type === 'current-month'
      if (day === 0 || day === 6) {
        return bol && true
      } else {
        return false
      }
    },
    changeDate() {
      this.$emit('input', new Date(`${this.year}-${this.month}-01`))
    },
    changDay(day) {
      this.$emit('input', new Date(day))
    }
  }
}
</script>

<style lang="scss" scoped>
.calendar {
  ::v-deep .el-calendar__header {
    display: none;
  }
}
</style>
